package com.example.dragandview.frag;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.dragandview.R;


public class FragmentActivity extends AppCompatActivity {
    private Button btn_change,btn_remove;
    private TopFragment topfrag = new TopFragment() ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);
        btn_change = findViewById(R.id.btn_change);
        btn_remove = findViewById(R.id.btn_remove);

        btn_change.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_top,new BottomFragment()).commit();
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_bottom,topfrag).commit();
            }
        });
        btn_remove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getSupportFragmentManager().beginTransaction().remove(topfrag).addToBackStack(null).commit();
            }
        });
        //代码实现
//        FragmentManager fragmentManager = getSupportFragmentManager();
//        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
//        fragmentTransaction.add(R.id.fragment_top, topfrag);
//        fragmentTransaction.add(R.id.fragment_bottom,new BottomFragment());
//        fragmentTransaction.commit();




        //Activity中获取fragment的控件
//        TopFragment topFragment = (TopFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_top);
//        TextView textView = (TextView)topFragment.getView().findViewById(R.id.tv_top);
//        textView.setText("55555555555");
//        BottomFragment bottomFragment = (BottomFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_bottom);
//        Button button = bottomFragment.getView().findViewById(R.id.btn_fragment_bottom);
//        TextView textView1 = bottomFragment.getView().findViewById(R.id.tv_text_bottom);
//        button.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                textView1.setText("444444444");
//                textView.setText("333333333333333");
//            }
//        });

    }
}
